package demo;
//自定义线程池
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingDeque;
import java.util.concurrent.BlockingQueue;

class myBolckqueue {
    //用阻塞队列存数据
    private BlockingQueue<Runnable> queue = null;

    //构造方法
    public myBolckqueue(int n) {
        //初始化任务容量
        queue = new ArrayBlockingQueue<Runnable>(1000);

        //线程数
        for (int i = 0; i < n; i++) {
            //创建线程执行
            Thread t = new Thread(() -> {
                try {
                    while (true) {
                        Runnable task = queue.take();
                        task.run();
                    }
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            });
            t.start();
        }
    }

    public void submit(Runnable task) throws InterruptedException {
        queue.put(task);
    }
}
    //自定义线程池
public class Test15 {
    public static void main(String[] args) throws InterruptedException {
        myBolckqueue pool = new myBolckqueue(1);
        for (int i = 0; i < 100; i++) {
            int id = i;
            //向队队列存放任务
            pool.submit(()->{
                System.out.println(Thread.currentThread().getName()+"id = "+id);
            });
        }

    }
}
